package com.xwj.service.Impl;

import com.xwj.dao.RaRootDao;
import com.xwj.entity.RaRoot;
import com.xwj.service.Interface.RaRootService;
import com.xwj.util.RedisUtils.annotations.RedisCacheable;
import com.xwj.util.SpringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by thinkjoy on 2017/7/12.
 */
@Slf4j
@Service
@CacheConfig(cacheNames = "RaRootCache")
public class RaRootServiceImpl implements RaRootService {

    @Autowired
    private RaRootDao raRootDao;

    public static RaRootServiceImpl getInstance() {
        return SpringUtils.getBean(RaRootServiceImpl.class);
    }

    @Override
    @Cacheable(key = "#root.methodName")
    public List<RaRoot> findAllRoot() {
        try {
            return raRootDao.findAllRoot();
        } catch (NullPointerException e) {
            log.error("RaRootService异常:", e);
            e.printStackTrace();
            return Collections.emptyList();
        }
    }

    @Override
    @Cacheable(key = "'root_'+#id")
    public RaRoot findRootById(int id) {
        RaRoot raRoot;
        try {
            raRoot = raRootDao.findRootById(id);
        } catch (NullPointerException e) {
            log.error("RaRootService异常:", e);
            raRoot = null;
        }
        return raRoot;
    }

    @Override
    @Cacheable(key = "#root.methodName + #ids")
    public List<RaRoot> findRootByIds(List ids) {
        try {
            return raRootDao.findRootByIds(ids);
        } catch (Exception e) {
            log.error("RaRootService异常:", e);
            return Collections.emptyList();
        }
    }

    @Override
    @CacheEvict(allEntries = true)
    @Transactional(isolation = Isolation.READ_COMMITTED)
    public int addRoot(String name, String desc) {
        try {
            Map<String, Object> map = new HashMap<>();
            map.put("root", name);
            map.put("desc", desc);
            return raRootDao.addRoot(map);
        } catch (Exception e) {
            log.error("RaRootService异常:", e);
            return 0;
        }
    }

    @Override
    @CacheEvict(allEntries = true)
    @Transactional(isolation = Isolation.READ_COMMITTED)
    public int updateRootById(RaRoot raRoot) {
        try {
            return raRootDao.updateRootById(raRoot);
        } catch (Exception e) {
            log.error("RaRootService异常:", e);
            return 0;
        }

    }

    @Override
    @CacheEvict(allEntries = true)
    @Transactional(isolation = Isolation.READ_COMMITTED)
    public int deleteRootById(int id) {
        try {
            return raRootDao.deleteRootById(id);
        } catch (Exception e) {
            log.error("RaRootService异常:", e);
            return 0;
        }
    }

    @RedisCacheable(key = "testKey")
    public RaRoot test() {
        return raRootDao.findRootById(1);
    }
}
